#include "stdafx.h"
#include "PortalBrowser.h"
#include "RequestDumperWnd.h"
#include "WndResizer.h"

CRequestDumperWnd::CRequestDumperWnd(HWND hCallbackWnd):
	m_pResizer(NULL),
	m_hCallbackWnd(hCallbackWnd)
{

}

CRequestDumperWnd::~CRequestDumperWnd()
{
	if (m_pResizer)
	{
		delete m_pResizer;
		m_pResizer = NULL;
	}
}

LRESULT CRequestDumperWnd::OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	UNREFERENCED_PARAMETER(uMsg);
	UNREFERENCED_PARAMETER(wParam);
	UNREFERENCED_PARAMETER(lParam);
	UNREFERENCED_PARAMETER(bHandled);
	
	m_pResizer = new CWndResizer(GetProcessHeap(), 3, m_hWnd);
	
	m_pResizer->AddItem(m_hWnd, IDC_BTN_COPY, IWndResizer::RF_ABSOLUTE_STICK_BOTTOM);
	m_pResizer->AddItem(m_hWnd, IDOK, IWndResizer::RF_ABSOLUTE_STICK_RIGHT_BOTTOM);
	m_pResizer->AddItem(m_hWnd, IDC_LIST_REQUEST, IWndResizer::RF_ABSOLUTE_STICK_ALL);
	
	RECT rect;
	GetClientRect(&rect);
	int iColW = (rect.right - rect.left) / 2;
	m_List.Attach(GetDlgItem(IDC_LIST_REQUEST));
	m_List.InsertColumn(0, L"URL", LVCFMT_LEFT, iColW);
	m_List.InsertColumn(1, L"POST data", LVCFMT_LEFT, iColW);
	m_List.InsertColumn(2, L"Headers", LVCFMT_LEFT, iColW);
	
	m_List.SetExtendedListViewStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_LABELTIP);
	
	return TRUE;
}

LRESULT CRequestDumperWnd::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	UNREFERENCED_PARAMETER(uMsg);
	UNREFERENCED_PARAMETER(wParam);
	UNREFERENCED_PARAMETER(bHandled);
	
	if (m_pResizer)
		m_pResizer->Resize(LOWORD(lParam), HIWORD(lParam));
		
	return true;
}

void CRequestDumperWnd::AddRequest(LPCWSTR pwcURL, LPCWSTR pwcHeaders, LPCWSTR pwcPOSTData)
{
	const int iCount = m_List.GetItemCount();
	
	int ind = m_List.AddItem(iCount, 0, pwcURL);
	
	if (pwcPOSTData && *pwcPOSTData)
		m_List.SetItemText(ind, 1, pwcPOSTData);
		
	if (pwcHeaders && *pwcHeaders)
	{
		std::wstring strHdr = pwcHeaders;
		if (strHdr.size() >= 2 && wcscmp(&strHdr.at(strHdr.size() - 2), L"\r\n") == 0)
			strHdr.resize(strHdr.size() - 2);
			
		if (strHdr.size())
			m_List.SetItemText(ind, 2, strHdr.c_str());
	}
}

LRESULT CRequestDumperWnd::OnClose(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
{
	UNREFERENCED_PARAMETER(uMsg);
	UNREFERENCED_PARAMETER(wParam);
	UNREFERENCED_PARAMETER(lParam);
	UNREFERENCED_PARAMETER(bHandled);
	
	::PostMessage(m_hCallbackWnd, WM_CLOSE_REQUEST, 0, 0);
	
	return true;
}

LRESULT CRequestDumperWnd::OnBtnOK(WORD /* wNotifyCode */, WORD /* wID */, HWND /* hWndCtl */, BOOL& /* bHandled */)
{
	::PostMessage(m_hCallbackWnd, WM_CLOSE_REQUEST, 0, 0);
	
	return true;
}

LRESULT CRequestDumperWnd::OnCopy(WORD /* wNotifyCode */, WORD /* wID */, HWND /* hWndCtl */, BOOL& /* bHandled */)
{
	std::wstring str;
	
	if (!OpenClipboard())
		return FALSE;
		
	EmptyClipboard();
	
	const int iColCount = m_List.GetHeader().GetItemCount();
	int ind = 0;
	
	while ((ind = m_List.GetNextItem(--ind, LVNI_SELECTED)) != -1)
	{
		size_t curlen = str.size();
		size_t curlen_per_item = curlen;
		
		for (int j = 0; j < iColCount; j++)
		{
			str.resize(curlen + 256);
			str.resize(curlen + m_List.GetItemText(ind, j, &str.at(curlen), 256));
			if (curlen != str.size())
			{
				str += L"\r\n";
				curlen = str.size();
			}
		}
		
		if (curlen_per_item != str.size())
		{
			str += L"\r\n";
		}
	}
	
	if (str.size())
	{
		HGLOBAL hglbCopy = GlobalAlloc(GMEM_MOVEABLE, (str.size() + 1) * sizeof(WCHAR));
		if (hglbCopy == NULL)
		{
			CloseClipboard();
			return FALSE;
		}
		
		LPWSTR lptstrCopy = (LPWSTR)GlobalLock(hglbCopy);
		wcscpy_s(lptstrCopy, str.size() + 1, str.c_str());
		GlobalUnlock(hglbCopy);
		
		SetClipboardData(CF_UNICODETEXT, hglbCopy);
	}
	
	CloseClipboard();
	return TRUE;
}